<template>
  <div v-if="show">
    <el-dialog :visible.sync="dialogVisible" fullscreen append-to-body>
      <el-container>
        <el-main style="height:calc(100vh  - 160px); padding: 0 45px;">
          <div class="score">
            <el-button type="primary" @click="exportExcel(cepingHdglb.hdName +'_会议')">导出统计表</el-button>
          </div>
          <div class="customTableHeader">
            <div class="customTableTitle">{{ cepingHdglb.hdName }}</div>
            <div class="customTableSummary flex justifyContentSpaceBetween">
              <div>
                测评时间：{{ $moment(cepingHdglb.hdKssj).format("YYYY年MM月DD日") }}
              </div>
              <div class="flex fontweight blue">
                测评率：{{ cpl }}%
              </div>
            </div>
          </div>
          <div class="cpTable">
            <el-table ref='hdhz-table' :data="tableData" border height="calc(100vh - 287px)">
              <el-table-column label="序号" width="100" type="index">
              </el-table-column>
              <el-table-column label="被测评对象" min-width="200" header-align="center" align="center">
                <template slot-scope="scope">
                  {{ scope.row.bcpDx == '部门' ? scope.row.bcpDeptName : scope.row.bcpUserName }}
                </template>
              </el-table-column>
              <!--              <el-table-column prop="bcpUserName" label="姓名" min-width="200" header-align="center" align="center"-->
              <!--                               v-if="cepingHdglCppmxbInfo.cpgzBpfdx=='个人'">-->
              <!--              </el-table-column>-->
              <el-table-column :prop="'zb'" :label="t0.cppName" min-width="120" header-align="center" align="center"
                               v-for="(t0,i0) in cepingHdglCppmxbList" :key="i0*1000000">
                <el-table-column :prop="'zb_'+t0.cppId+'_'+t1.id" :label="t1.zbMc" min-width="120" header-align="center"
                                 align="center"
                                 v-for="(t1,i1) in t0.cepingXmmxbListTree" :key="i1*10000">
                  <el-table-column :prop="'zb_'+t0.cppId+'_'+t2.id" :label="t2.zbMc" min-width="120"
                                   header-align="center"
                                   align="center"
                                   v-if="t1.children"
                                   v-for="(t2,i2) in t1.children" :key="i1*10000+i2*100">
                    <el-table-column :prop="'zb_'+t0.cppId+'_'+t3.id" :label="t3.zbMc" min-width="120"
                                     header-align="center"
                                     align="center" v-if="t2.children"
                                     v-for="(t3,i3) in t2.children" :key="i1*10000+i2*100+i3">
                    </el-table-column>
                  </el-table-column>
                </el-table-column>
                <el-table-column :prop="'cpFz_'+t0.cppId" label="指标汇总" min-width="120" header-align="center"
                                 align="center">
                </el-table-column>
                <el-table-column :prop="'cpDj_'+t0.cppId" label="评价等次" min-width="120" header-align="center"
                                 align="center"
                                 v-if="t0.djxxDjmx!=''">
                </el-table-column>
                <el-table-column :prop="'cpZf_'+t0.cppId" label="评测成绩" min-width="120" header-align="center"
                                 align="center"
                                 v-if="t0.djxxDjmx!=''">
                </el-table-column>
                <el-table-column :prop="'qzZf_'+t0.cppId" label="分组成绩" min-width="120" header-align="center"
                                 align="center">
                </el-table-column>
              </el-table-column>
              <el-table-column prop="hdFz" label="最终成绩" min-width="120" header-align="center" align="center">
                <template slot-scope="scope">
                  <span style="color: #3e51a3;cursor: pointer;" @click="showMx(scope.row)">
                    {{ scope.row.hdFz }}
                  </span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-main>
      </el-container>
      <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog title="明细查询" :visible.sync="mxDialogVisible" :width="getDjmx()?'1800px':'1200px'" append-to-body>
      <div style="margin-bottom:20px">
        <el-table ref="zbABTable" :data="mxList" border stripe height="400px">
          <el-table-column label="序号" width="100" type="index" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="cpCode" label="票种" min-width="60" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpCode }}票
            </template>
          </el-table-column>
          <el-table-column prop="cpCsl" label="总票数" min-width="80" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpCsl }}票
            </template>
          </el-table-column>
          <el-table-column prop="cpDsl" label="提交票数" min-width="100" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpDsl }}票
            </template>
          </el-table-column>
          <el-table-column prop="cpQz" label="票种权重" min-width="100" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpQz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cpQzpjf" label="平均分数" min-width="120" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="cpqz" label="测评项权重" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
            <template slot-scope="scope">
              {{ scope.row.cpqz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cpQzpjfCph" label="测评项分数" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
          </el-table-column>
          <el-table-column prop="dcqz" label="等次项权重" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
            <template slot-scope="scope">
              {{ scope.row.dcqz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cpQzpjfDch" label="等次项分数" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
          </el-table-column>
          <el-table-column prop="cpQzpjfZzfs" label="测评分数" min-width="100" header-align="center" align="center"
                           v-if="getDjmx()">
          </el-table-column>
          <el-table-column prop="cppQz" label="测评票权重" min-width="120" header-align="center" align="center"
                           v-if="getCppQz()">
            <template slot-scope="scope">
              {{ scope.row.cppQz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cppFz" label="最终分数" min-width="100" header-align="center" align="center"
                           v-if="getCppQz()">
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="exportExcelAB">导出</el-button>
        <el-button type="primary" plain @click="mxDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {hzInfo} from '@/api/ceping/cepinghdglhdzhhzb'
import {bcpHdInfo} from '@/api/ceping/cepinghdglhdcphzb'
import FileSaver from 'file-saver'
import XLSX from 'xlsx'

export default {
  name: "cepinghdzhhzDialog",
  data() {
    return {
      show: false,
      dialogVisible: false,
      mxDialogVisible: false,
      cepingHdglb: {},
      cepingHdglCppmxbList: [],
      cepingXmmxbList: [],
      cepingHdglHdzhhzbList: [],
      cepingHdglHdcphzbList: [],
      cepingHdglHdhzmxbList: [],
      tableData: [],//指标结构
      cpl: 0,
      mxList: []
    }
  },
  methods: {
    init(hdId) {
      this.cpl = 0;
      this.show = false;
      hzInfo(hdId).then(res => {
       //console.log(res.data.data)
        this.cepingHdglb = res.data.data.cepingHdglb;
        this.cepingHdglCppmxbList = res.data.data.cepingHdglCppmxbList;
        this.cepingXmmxbList = res.data.data.cepingXmmxbList;
        this.cepingHdglHdcphzbList = res.data.data.cepingHdglHdcphzbList;
        this.cepingHdglHdzhhzbList = res.data.data.cepingHdglHdzhhzbList;
        this.cepingHdglHdhzmxbList = res.data.data.cepingHdglHdhzmxbList;


        for (var i in this.cepingHdglCppmxbList) {
          this.cepingHdglCppmxbList[i].cepingXmmxbListTree = this.getTree(this.cepingXmmxbList[i], 0);
        }
       //console.log(this.cepingHdglCppmxbList)

        let data = this.cepingHdglHdzhhzbList;

        //设置表格数据
        let Zps = 0;
        let Dps = 0;
        for (var i in this.cepingHdglHdcphzbList) {
          var info = this.cepingHdglHdcphzbList[i];
          Zps += info.cpZps;
          Dps += info.cpDps;
        }
        if (Zps == 0 || Dps == 0) {
          this.cpl = 0;
        } else {
          this.cpl = Math.round(Dps / Zps * 100 * 100) / 100;
        }

        //填表
        let dataObj = {};

        for (var i in data) {
          dataObj[(data[i].bcpDx == '部门' ? "dept" : "user") + "_" + (data[i].bcpDx == '部门' ? data[i].bcpDeptId : data[i].bcpUserId)] = data[i];
        }
       //console.log(dataObj)

        for (var i in this.cepingHdglHdcphzbList) {
          var info = this.cepingHdglHdcphzbList[i];
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpFz_" + info.cppId] = info.cpFz;
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpDj_" + info.cppId] = info.cpDj;
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpZf_" + info.cppId] = info.cpZf;
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["qzZf_" + info.cppId] = info.qzZf;
        }

        for (var i in this.cepingHdglHdhzmxbList) {
          var info = this.cepingHdglHdhzmxbList[i];
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["zb_" + info.cppId + "_" + info.zbId] = info.zbFz;
        }


        for (var i in data) {
          data[i] = dataObj[(data[i].bcpDx == '部门' ? "dept" : "user") + "_" + (data[i].bcpDx == '部门' ? data[i].bcpDeptId : data[i].bcpUserId)];
        }

        this.tableData = data;

        this.show = true;
        this.dialogVisible = true;
      })
    },
    getTree(ary, pid) {
      return ary.filter(v => {
        if (v.pid === pid) {
          if (v.pid === 0) {
            v.xh = 1;
          } else {
            for (var a = 0; a < ary.length; a++) {
              if (ary[a].id == v.pid) {
                v.xh = ary[a].xh + 1;
              }
            }
          }
          return v
        }
      }).map(v => {
        const children = this.getTree(ary, v.id);
        if (children.length) v.children = this.getTree(ary, v.id);
        return v
      });
    },
    /**
     * excel 导出
     */
    exportExcel(excelName) {
      try {
        const $e = this.$refs['hdhz-table'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }
        const wb = XLSX.utils.table_to_book($table, {raw: true})

        const wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: true, type: 'array'})
        FileSaver.saveAs(
          new Blob([wbout], {type: 'application/octet-stream'}),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    },
    showMx(row) {
      this.mxList = [];
     //console.log(row)
      bcpHdInfo(row.hdId, row.bcpDx == "部门" ? row.bcpDeptId : row.bcpUserId, row.bcpDx == "部门"?2:1).then(res => {
        this.mxList = res.data.data;
        this.mxDialogVisible = true;
      });
    },
    getDjmx() {
      let r = false;
      for (var i in this.mxList) {
        if (this.mxList[i].cpqz != 100) {
          return true;
        }
      }
      return r;
    },
    getCppQz() {
      let r = false;
      for (var i in this.mxList) {
        if (this.mxList[i].cppQz != 100) {
          return true;
        }
      }
      return r;
    },
    exportExcelAB() {
      const excelName = this.cepingHdglb.hdName + "_明细"

      try {
        const $e = this.$refs['zbABTable'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }
        const wb = XLSX.utils.table_to_book($table, {
          raw: true
        })

        const wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream'
          }),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    }
  }
}
</script>

<style scoped>

</style>
